<template>
  <div>
    <div>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item>
          <el-input
            v-model="formInline.title"
            placeholder="请输入标题关键字"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit" icon="el-icon-search"
            >查找</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="botbtn">
      <el-button type="primary" @click="readsun" icon="el-icon-edit"
        >批量已读</el-button
      ><el-button type="danger" @click="delsun" icon="el-icon-delete"
        >批量删除</el-button
      >
    </div>
    <div>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="未读通知" name="unread"> </el-tab-pane>

        <el-tab-pane label="已读通知" name="read"></el-tab-pane>
        <el-tab-pane label="所有通知" name="all"></el-tab-pane>
      </el-tabs>
      <el-table
        v-loading="loading"
        element-loading-text="正在查询中..."
        ref="multipleTable"
        :data="tableData1"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column
          label="通知标题"
          prop="noticeTitle"
          min-width="300"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="updateTime"
          label="通知时间"
          align="center"
          min-width="100"
        >
        </el-table-column>
        <el-table-column
          prop="deleted"
          label="通知状态"
          show-overflow-tooltip
          align="center"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.readTime == null" type="primary"
              >未读</el-tag
            >
            <el-tag v-else type="success">已读</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" show-overflow-tooltip align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >阅读</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="get.page"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :page-size="get.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <!-- ====================阅读弹框====================== -->
    <el-dialog
      :title="title"
      :visible.sync="centerDialogVisible"
      width="50%"
      center
    >
      <el-divider content-position="left"
        >{{ admin }} 于 {{ time }} 通知如下内容：</el-divider
      >
      <div v-html="content"></div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="centerDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listNotice,
  catNotice,
  brmNotice,
  bcatNotice,
  rmotice,
} from "@/api/profile";
export default {
  data() {
    return {
      loading: false,
      centerDialogVisible: false,
      activeName: "unread",
      formInline: {},
      tableData1: [],
      tableData2: [],
      tableData3: [],
      get: {
        page: 1,
        limit: 20,
        title: "",
        type: "unread",
        sort: "add_time",
        order: "desc",
      },
      total: 0,
      title: "",
      time: "",
      admin: "",
      content: "",
      ids: "",
    };
  },
  created() {
    this.getdata();
  },
  methods: {
    // 默认接口
    getdata() {
      listNotice(this.get).then((res) => {
        if (res.errno == 0) {
          this.tableData1 = res.data.list;
          this.total = res.data.total;
        }
        // console.log(res);
      });
    },
    // 切换标签
    handleClick(tab, event) {
      console.log(tab.index, event);
      if (tab.index == 0) {
        this.get.type = "unread";
        this.getdata();
      } else if (tab.index == 1) {
        this.get.type = "read";
        this.getdata();
      } else {
        this.get.type = "all";
        this.getdata();
      }
    },
    // 搜索
    onSubmit() {
      this.get.page = 1;
      this.get.title = this.formInline.title;
      this.getdata();
    },
    // 多选
    handleSelectionChange(res) {
      this.ids = [];
      this.ids.push(
        res.map((item) => {
          return item.id;
        })
      );
      console.log(this.ids);
    },
    // 阅读按钮
    handleEdit(index, row) {
      this.centerDialogVisible = true;
      catNotice(row).then((res) => {
        console.log(res);
        this.title = res.data.title;
        this.time = res.data.time;
        this.admin = res.data.admin;
        this.content = res.data.content;
        this.getdata();
      });
      // console.log(index, row);
    },
    // 删除按钮
    handleDelete(index, row) {
      console.log(index, row);
      rmotice(row).then((res) => {
        if (res.errno == 0) {
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
          });
          this.getdata();
        }
      });
      // this.dels([row.id]);
    },
    // 批量已读
    readsun() {
      this.reads(...this.ids);
    },

    // 已读调用
    reads(ids) {
      let data = {
        ids: ids,
      };
      bcatNotice(data).then((res) => {
        // console.log(res);
        if (res.errno == 0) {
          this.$notify({
            title: "成功",
            message: "设置批量已读成功",
            type: "success",
          });
          this.getdata();
        }
      });
    },

    // 批量删除
    delsun() {
      this.dels(...this.ids);
    },
    //删除调用
    dels(ids) {
      let data = {
        ids: ids,
      };
      brmNotice(data).then((res) => {
        console.log(res);
        if (res.errno == 0) {
          this.$notify({
            title: "成功",
            message: "设置批量删除成功",
            type: "success",
          });
          this.getdata();
        }
      });
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.get.limit = val;
      this.getdata();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.get.page = val;
      this.getdata();
    },
  },
};
</script>

<style lang="scss" scoped>
.botbtn {
  display: flex;
  justify-content: right;
}
</style>